<?php
/**
 * 广告模型
 *
 *@author hary <hary@taomee.com>
 */
class ads_modal_model extends model
{
	private $ad_modal_type;

	/**
	 * 构造函数
	 * @return max
	 */
	function __construct()
	{
		parent::__construct();

		$this->ad_modal_type = $this->sys_conf_var[ 'table' ][ 'ad_modal_type' ];
	}


	/**
	 * 根据id获取ads的类型信息
	 * @param $id
	 * @return max
	 */
	public function getModalById($id){

		$id = $this->formatData($id);

		$this->where($id,'mt_id','=','and',0);

		$mt = $this->getDataInfo($this->ad_modal_type);

		return $mt;
	}

	/**
	 * 添加ads的类型
	 * @param $type
	 * @return boolean
	 */
	public function addModal($type){
		
		$type = $this->formatData($type);

		if(!is_array($type)) return false;

		$this->addDataBatch2($type,array('mt_name','mt_des','mt_formwork_name','mt_is_multi','sort'));

		return $this->dataInsert($this->ad_modal_type);

	}

	/**
	 * 获取模板类型
	 * @return max
	 */
	public function getModals(){
		return $this->getDataList($this->ad_modal_type);
	}

	/**
	 * 根据id删除ads的类型
	 * @param $id
	 * @return boolean
	 */
	public function delModalById($id){

		$id = intval($id);

		$this->where('mt_id',$id,'=','and',0);

		return $this->dataDel($this->ad_modal_type);

	}

	/**
	 * 修改ads的类型信息
	 * @param $type
	 * @return boolean
	 */
	public function changeModal($type){

		$type = $this->formatData($type);

		if(!is_array($type)) return false;

		$id = $type['mt_id'];

		$this->addDataBatch2($type,array('mt_name','mt_des','mt_formwork_name','mt_is_multi','sort'));

		$this->where('mt_id',$id,'=','and',0);

		return $this->dataUpdate($this->ad_modal_type);
	}

	/**
	 * 根据条件获取模本类型
	 * @param $keywords
	 * @param $page
	 * @param $page_size
	 * @param $order
	 * @return unknown_type
	 */
	public function getModalsByKeywords($keywords,$page='1',$page_size='6',$order='sort desc'){

		$keywords = $this->formatData($keywords);

		if(empty($keywords))
		{
			$sql = "select * from $this->ad_modal_type ";
		}
		else
		{
			$sql = "select * from $this->ad_modal_type where 
	   				mt_name like '%$keywords%' or 
	   				mt_des like '%$keywords%' or 
	   				mt_formwork_name   like '%$keywords%' or 
	   				mt_is_multi   like '%$keywords%'";
		}
			
		if($page) $this->setPage($page);
			
		if($page_size) $this->setLimit($page_size);
			
		if($order) $this->setOrder('order by '.$order);
			
		return $this->customQuery($sql);
	}


}